Skip to content

Added ensure_* functions #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

pegasd
Copy link
Contributor

@pegasd pegasd commented Dec 25, 2017

After seeing lots of these

if $ensure == present {
  $ensure_directory = directory
} else {
  $ensure_directory = absent
}

file { '/somedir':
  ensure => $ensure_directory,
}

I decided to implement helper functions and abstract this logic away.

Usage is as follows:

file { '/somedir':
  ensure => ensure_directory($ensure),
}

file { '/somedir/somelink':
  ensure => ensure_link($ensure),
}

@pegasd
Copy link
Contributor Author

pegasd commented Dec 25, 2017

Hm, doesn't seem like broken tests were caused by my changes.
Did something break on Travis / rspec side?

@glennsarti
Copy link
Contributor

@pegasd I'm seeing similar failures in my windows modules on Travis :-( Suspect Travis just us a christmas present :-(

@glennsarti
Copy link
Contributor

@pegasd Tracking this in https://tickets.puppetlabs.com/browse/MODULES-6339

@pegasd
Copy link
Contributor Author

pegasd commented Feb 22, 2018

Can we retest this please? I'm assuming the issue has been fixed.

Copy link
Contributor

@hlindberg hlindberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this belongs in stdlib. Please publish this in your own module and namespace the function.

# }
# }
dispatch :ensure do
param 'Variant[String, Boolean]', :ensure_param
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the type to Enum[present, absent] you would not need to check that in an else since you are guaranteed your method gets called with only one of those two values.

@hlindberg
Copy link
Contributor

The original can be written: $ensure_directory = if $ensure == present { directory } else { $ensure }.
If you want to use these in your codebase consider that the functions are simple enough that they can be written in the puppet language - but do put them in a module of your own and namespace them.

A more useful general purpose function would be something like a simple switch/swap value like:
swap_matching($ensure, present => directory) that would swap the value in $ensure to 'directory' if value is 'present', else return the value of $ensure. This function would be comparable to using a case expression (you give the function a hash of matches instead of writing a case expression). I am however not convinced that such a function is that desirable since you have to learn that it exists and what it does - in contrast to $ensure_directory = if $ensure == present { directory } else { $ensure } which is general logic that you need to learn anyway.

Closing this for now since commits and PR also must be marked with the ticket number.
In any case, please open a Jira ticket for discussing the merits of adding this (the original, or the idea of having a general swap/switch) to stdlib.

@hlindberg hlindberg closed this Feb 22, 2018
b4ldr added a commit to b4ldr/puppetlabs-stdlib that referenced this pull request Dec 16, 2020
This CR adds a new function to case the ensureable property to a resource
specific value. e.g.
  * stdlib::ensure('present', 'service') == 'running'
  * stdlib::ensure('present', 'directory') == 'directory'
This is usefull when you want to pass ensure to a custome class and
ensure all the resource get the correct value e.g.

```
class foo(
  Enum['present', 'absent'] $ensure = 'present'
) {
  file {'/some/dir':
    ensure => stdlib::ensure($ensure, 'directory')
  }
  file {'/some/file':
    ensure => stdlib::ensure($ensure, 'file')
  }
  file {'/some/link':
    ensure => stdlib::ensure($ensure, 'link')
  }
  service {'some-service':
    ensure => stdlib::ensure($ensure, 'service')
  }
```

something similar to this was discussed in puppetlabs#869
b4ldr added a commit to b4ldr/puppetlabs-stdlib that referenced this pull request Dec 17, 2020
This CR adds a new function to case the ensureable property to a resource
specific value. e.g.
  * stdlib::ensure('present', 'service') == 'running'
  * stdlib::ensure('present', 'directory') == 'directory'
This is usefull when you want to pass ensure to a custome class and
ensure all the resource get the correct value e.g.

```
class foo(
  Enum['present', 'absent'] $ensure = 'present'
) {
  file {'/some/dir':
    ensure => stdlib::ensure($ensure, 'directory')
  }
  file {'/some/file':
    ensure => stdlib::ensure($ensure, 'file')
  }
  file {'/some/link':
    ensure => stdlib::ensure($ensure, 'link')
  }
  service {'some-service':
    ensure => stdlib::ensure($ensure, 'service')
  }
```

something similar to this was discussed in puppetlabs#869
b4ldr added a commit to b4ldr/puppetlabs-stdlib that referenced this pull request Jan 4, 2021
This CR adds a new function to case the ensureable property to a resource
specific value. e.g.
  * stdlib::ensure('present', 'service') == 'running'
  * stdlib::ensure('present', 'directory') == 'directory'
This is usefull when you want to pass ensure to a custome class and
ensure all the resource get the correct value e.g.

```
class foo(
  Enum['present', 'absent'] $ensure = 'present'
) {
  file {'/some/dir':
    ensure => stdlib::ensure($ensure, 'directory')
  }
  file {'/some/file':
    ensure => stdlib::ensure($ensure, 'file')
  }
  file {'/some/link':
    ensure => stdlib::ensure($ensure, 'link')
  }
  service {'some-service':
    ensure => stdlib::ensure($ensure, 'service')
  }
```

something similar to this was discussed in puppetlabs#869
b4ldr added a commit to b4ldr/puppetlabs-stdlib that referenced this pull request Jun 23, 2022
This CR adds a new function to case the ensureable property to a resource
specific value. e.g.
  * stdlib::ensure('present', 'service') == 'running'
  * stdlib::ensure('present', 'directory') == 'directory'
This is usefull when you want to pass ensure to a custome class and
ensure all the resource get the correct value e.g.

```
class foo(
  Enum['present', 'absent'] $ensure = 'present'
) {
  file {'/some/dir':
    ensure => stdlib::ensure($ensure, 'directory')
  }
  file {'/some/file':
    ensure => stdlib::ensure($ensure, 'file')
  }
  file {'/some/link':
    ensure => stdlib::ensure($ensure, 'link')
  }
  service {'some-service':
    ensure => stdlib::ensure($ensure, 'service')
  }
```

something similar to this was discussed in puppetlabs#869
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants